/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package proyectoso;

import java.util.concurrent.Semaphore;

/**
 *
 * @author Christian
 */
public class Boat {
    int people;
    Boat(){
        
    }
    public void begin(int mc,int oc,int ma,int oa){
        Bote b = new Bote(0);
        Arena ar = new Arena();
        this.people=mc+oc+ma+oa;
        Semaphore sem= new Semaphore (2,true);
        Avisar av= new Avisar();
        int i,j;
        Child [] c = new Child[mc+oc];
        Adult [] a = new Adult[ma+oa];
        for (i=0;i<ma;i++){
            a[i]=new Adult(1,b,ar,sem,av);
            new Thread(a[i]).start();
        }
        for (j=i;j<oa+ma;j++){
            a[j]=new Adult(0,b,ar,sem,av);
            new Thread(a[j]).start();
        }
        for (i=0;i<mc;i++){
            c[i]=new Child(1,b,ar,sem,av);
            new Thread(c[i]).start();
        }
        for (j=i;j<oc+mc;j++){
            c[j]=new Child(0,b,ar,sem,av);
            new Thread(c[j]).start();
        }
        while(!av.fin){
            synchronized(av){
                try{
                    if(!av.fin)
                    av.wait();
                }
                catch(Exception e){
                
                }
            }
            if(Revisar(ar)){
            
            }
            else
                av.Reset();
        }
    }
    public boolean Revisar(Arena a){
        if (a.GetPeople(1)==people)
            return true;
        else
            return false;
    }
}
